package com.kt.mes.service.impl;

import java.util.Date;
import java.util.List;

import com.kt.mes.domain.*;
import com.kt.mes.mapper.StorageInRecordMapper;
import com.kt.mes.service.IOrderProductService;
import com.kt.mes.service.IProductStoreService;
import com.kt.mes.service.IStorageOutRecordService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kt.mes.mapper.ProductStoreListMapper;
import com.kt.mes.service.IProductStoreListService;

/**
 * 成品库存储Service业务层处理
 * 
 * @author lzc
 * @date 2021-01-23
 */
@Service
public class ProductStoreListServiceImpl implements IProductStoreListService 
{
    @Autowired
    private IOrderProductService orderProductService;
    @Autowired
    private ProductStoreListMapper productStoreListMapper;
    @Autowired
    private IStorageOutRecordService storageOutRecordService;

    @Autowired
    private StorageInRecordMapper storageInRecordMapper;

    @Autowired
    private IProductStoreService productStoreService;

    /**
     * 查询成品库存储
     * 
     * @param id 成品库存储ID
     * @return 成品库存储
     */
    @Override
    public ProductStoreList selectProductStoreListById(Long id)
    {
        return productStoreListMapper.selectProductStoreListById(id);
    }

    /**
     * 查询成品库存储列表
     * 
     * @param productStoreList 成品库存储
     * @return 成品库存储
     */
    @Override
    public List<ProductStoreList> selectProductStoreListList(ProductStoreList productStoreList)
    {
        return productStoreListMapper.selectProductStoreListList(productStoreList);
    }

    /**
     * 新增成品库存储
     * 
     * @param productStoreList 成品库存储
     * @return 结果
     */
    @Override
    public int insertProductStoreList(ProductStoreList productStoreList)
    {
        productStoreList.setCreateTime(DateUtils.getNowDate());
        return productStoreListMapper.insertProductStoreList(productStoreList);
    }

    /**
     * 修改成品库存储
     * 
     * @param productStoreList 成品库存储
     * @return 结果
     */
    @Override
    public int updateProductStoreList(ProductStoreList productStoreList)
    {
        productStoreList.setUpdateTime(DateUtils.getNowDate());
        return productStoreListMapper.updateProductStoreList(productStoreList);
    }

    /**
     * 批量删除成品库存储
     * 
     * @param ids 需要删除的成品库存储ID
     * @return 结果
     */
    @Override
    public int deleteProductStoreListByIds(Long[] ids)
    {
        return productStoreListMapper.deleteProductStoreListByIds(ids);
    }

    /**
     * 删除成品库存储信息
     * 
     * @param id 成品库存储ID
     * @return 结果
     */
    @Override
    public int deleteProductStoreListById(Long id)
    {
        return productStoreListMapper.deleteProductStoreListById(id);
    }

    //根据库位号入库
    @Override
    public void takeInPS(Long areaNum,String oId){
        //查询订单
        OrderProduct orderProduct_query=new OrderProduct();
        orderProduct_query.setOpId(oId);
        List<OrderProduct> list_olist=  orderProductService.selectOrderProductList(orderProduct_query);

        if(list_olist.isEmpty()==false && list_olist.size()==1){

            OrderProduct order=list_olist.get(0);


            //入子表
            ProductStoreList productStoreList=new ProductStoreList();
            productStoreList.setTpsId(Long.valueOf(areaNum));//库位号
            productStoreList.setmId(order.getPpId()); //成品号
            productStoreList.setAvaQty(1L);
            productStoreList.setSumQty(1L);
            productStoreList.setCreateTime(new Date());
            productStoreList.setStatus("0");
            insertProductStoreList(productStoreList);

            //入库记录
            StorageInRecord storageInRecord=new StorageInRecord();
            storageInRecord.setRkId("RK"+DateUtils.dateTimeNow());
            storageInRecord.setmId(order.getPpId());
            storageInRecord.setRkQty(1);
            storageInRecord.setOrderId(oId);
            storageInRecord.setCreateTime(DateUtils.getNowDate());
            storageInRecord.setRkType("成品入库");
            storageInRecord.setRkPos(areaNum+" 号库位");
            storageInRecordMapper.insertStorageInRecord(storageInRecord);

            //更新主表状态
            ProductStore productStore=new ProductStore();
            productStore.setId(areaNum);
            productStore.setStatus("1");
            productStoreService.updateProductStore(productStore);


        }


    }

    @Override
    public int takeOutPS(Long id) {

        //获取存储信息
        ProductStoreList psl=  selectProductStoreListById(id);

        //添加出库记录
        StorageOutRecord storageOutRecord=new StorageOutRecord();
        storageOutRecord.setCkId("CK"+DateUtils.dateTime()+psl.getId()+"");
        storageOutRecord.setmId(psl.getmId());
        storageOutRecord.setCkType("取出成品");
        storageOutRecord.setCkQty(1L);
        storageOutRecord.setCkPos("成品库");
        storageOutRecord.setCreateTime(new Date());

        storageOutRecordService.insertStorageOutRecord(storageOutRecord);
        //删除物料
        deleteProductStoreListById(id);

        //修改主表状态
        ProductStoreList psl_query=new ProductStoreList();
        psl_query.setTpsId(psl.getTpsId());


        if (selectProductStoreListList(psl_query).isEmpty()) {
            ProductStore productStore=new ProductStore();
            productStore.setId(psl.getTpsId());
            productStore.setStatus("0");
            productStoreService.updateProductStore(productStore);

        }


        return 1;
    }
}
